package com.yfp.tissue.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.tissue.domain.entity.PositionDraw;
import com.yfp.tissue.domain.vo.PositionDrawVO;
import com.yfp.tissue.service.PositionDrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Description: 抽奖
 *
 * @CreateDate: 2024/9/19 11:49
 * @UpdateUser:
 * @UpdateDate: 2024/9/19 11:49
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/positionDraw")
@Tag(name = "点位抽奖管理")
public class PositionDrawController extends BaseController {
    @Autowired
    private PositionDrawService positionDrawService;

    @GetMapping("/findPage")
    @Operation(summary = "点位抽奖分页查询")
    public R findPage(String cityName) {
        startPage();
        PositionDraw positionDraw = new PositionDraw();
        positionDraw.setCityName(cityName);
        TableDataInfo dataTable = getDataTable(positionDrawService.findPage(positionDraw));
        return R.ok(dataTable);
    }

    @PostMapping("/derive")
    @Operation(summary = "点位抽奖列表数据导出", description = "数据导出")
    public void positionDrawExport(HttpServletResponse response, String cityName) {
        PositionDraw positionDraw = new PositionDraw();
        positionDraw.setCityName(cityName);
        List<PositionDrawVO> list = positionDrawService.findPage(positionDraw);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSerialNumber(i + 1);
        }
        ExcelUtil<PositionDrawVO> util = new ExcelUtil<PositionDrawVO>(PositionDrawVO.class);
        util.exportExcel(response, list, "抽奖列表数据");
    }

    @PostMapping("")
    @Operation(summary = "新增点位抽奖")
    public R insert(@RequestBody PositionDraw positionDraw) {
        positionDrawService.insert(positionDraw);
        return R.ok();
    }

    @PutMapping("")
    @Operation(summary = "编辑点位抽奖")
    public R edit(@RequestBody PositionDraw positionDraw) {
        positionDrawService.edit(positionDraw);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除点位抽奖")
    public R edit(@PathVariable("id") Long id) {
        positionDrawService.delById(id);
        return R.ok();
    }

    @GetMapping("/draw")
    @Operation(summary = "抽奖")
    public R draw() {
        List<PositionDrawVO> positionDrawVOS = positionDrawService.findByStatus(0);
        // 创建Random对象
        Random random = new Random();

        // 设定随机数范围的最小值和最大值
        // 最小值（包含
        int min = 0;
        // 最大值（不包含）
        int max = positionDrawVOS.size();
        // 生成随机数
        int randomNumber = random.nextInt(max - min) + min;
        PositionDrawVO positionDrawVO = positionDrawVOS.get(randomNumber);
        PositionDraw positionDraw = new PositionDraw();
        positionDraw.setId(positionDrawVO.getId());
        positionDraw.setStatus(1);
        positionDrawService.edit(positionDraw);
        return R.ok(positionDrawVO);
    }
}
